﻿//获取列表数据url
var baseUrl = "http://192.168.28.70:9090/api/GridEngine/GetGridDataInfo";
//数据分发系统：获取实时数据url
var currentInfoUrl = "http://210.12.220.215:8081/datapublish/getCurrentSensorData.html";
//数据分发系统：获取历史数据url
var historyInfoUrl = "http://210.12.220.215:8081/datapublish/getPeriodSensorData.html";

var points;

$(function () {
    if ($('.dab').length > 0) {
        $('html,body').addClass('hide');
    }
})

// 百度地图API功能
var map = new BMap.Map("allmap", { enableMapClick: false });
 var point = new BMap.Point(116.404, 39.915);
map.centerAndZoom(point, 15);
//map.disableDoubleClickZoom(true);
map.enableScrollWheelZoom(true);
var geoc = new BMap.Geocoder();
//添加路况信息控件
//var ctrl = new BMapLib.TrafficControl({
//    showPanel: false //是否显示路况提示面板
//});
//map.addControl(ctrl);
//ctrl.setAnchor(BMAP_ANCHOR_BOTTOM_RIGHT);


function showrealinfo() {
    $.ajax({
        type: "GET",
        url: baseUrl + "?GridCode=Grid201808211714540048&IsReturnRealData=true&OtherCondition=&OrderBy=&take=20&skip=0&page=1&pageSize=20",
        dataType: "json",
        error: function (x, e) {
            alert("未查询到车辆信息！");
        },
        async: true,
        // async: false,
        success: function (result) {  //回调函数，result，返回值  
            $("#roomshow").css('display', 'block');
            if (result.Result == true) {
                carArray = eval(result.DataJson);
                if (carArray != null) {
                    totalCarCount = carArray.length;
                    $("#roomcount").html(totalCarCount);
                    $("#accordion").html("");
                    $(carArray).each(function (index, object) {
                        //拼接左侧浮动框内容
                        var LeftHtml = '<div class="panel">'
                            + '<div class="panel-heading">'
                            + '    <h4 class="panel-title">'
                            + '        <a data-toggle="collapse" data-parent="#accordion" href="#collapse' + index + '" onclick="showcar(' + index + ');">'
                            + '            <div class="pull-left w-silder-titll">'
                            + '                <p class="pull-left cor-blue"><i class="iconfont  cor-blue">&#xe615;</i> <b>' + object.CarNumber + '</b></p>'
                            + '            </div>'
                            + '            <div class="pull-right w-silder-titrr">'
                            + '                <p class="pull-left"><i class="iconfont text-success"><b>&#xe603;</b></i><em class="font-12">正常</em></p>'
                            + '            </div>'
                            + '        </a> '
                            + '    </h4>'
                            + '</div>'
                            + '<div id="collapse' + index + '" class="panel-collapse collapse">'
                            + '    <div class="panel-body">'
                            + '        <div class="w-wotex">'
                            + '            <p><i class="iconfont w-icon"><b>&#xe60d;</b></i> ' + object.MainDriverName + ' <em>' + object.MainDriverTel + '</em></p>'
                            + '            <p><i class="iconfont w-icon">&#xe600;</i><em class="font-12" id="address' + index + '"></em></p>'
                            + '        </div>'
                            + '    </div>'
                            + '</div>'
                            + '</div>';                     
                        $("#accordion").append(LeftHtml);
                    });
                    //加载实时温湿度数据
                    //addCars(carArray, index);
                    InitCurrentData(carArray);
                }
                else {
                    alert("未查询到车辆信息！");
                }
            }
            else {
                alert("获取车辆信息时出错！");
            }
        },
        complete: function (x) {
        }
    });
}


$(function () {
    $("#allmap").height($(document).height() - 70);
    $("#menu-1").height($(document).height() - 70);
    $("#w-map-silder2").height($(document).height() - 70);
    $("#accordion").html("");
    points = new Array();
    showrealinfo();
});

//判断给定字符串是否包含在是否包含指定字符串
String.prototype.IsContain = function (str) {
    return (this.indexOf(str) > -1);
}


function InitCurrentData(carInfos) {
    cararr = carInfos;
    $.each(cararr, function (ind, va) {
        var htem, ltem, hhum, lhum, hc2h4, lc2h4, speed;
        var strhtem, strltem, strhhum, strlhum, strhc2h4, strlc2h4;
        var lat, lon;
        var sensors = va.sn;
        
        //根据sn号获取实时数据
        $.ajax({
            type: "GET",
            url: currentInfoUrl + "?SN=" + va.SensorId,
            dataType: 'json',
            async: true,
            //async: false,                                                 //设置为同步
            error: function (x, e) {
                //alert("未查询到实时数据！");
            },
            success: function (result) {
                var json = result;
                var pntlist = json.datalist;//回调函数，result，返回值
                if (pntlist.length != 0) {
                    var senlist = pntlist[0].valuelist;
                    var sensor;
                    var htem, ltem, hhum, lhum, temperature, humidity, lng, lat;        //最高最低温湿度、传感器实时温湿度 
                    $.each(senlist, function (i, tinfo) {
                        sensor = tinfo.fieldvalue;
                        //alert(JSON.stringify(senser));
                        if (htem == null) {
                            htem = sensor[0];
                        }
                        else if (htem < sensor[0]) {
                            htem = sensor[0];
                        }
                        if (ltem == null) {
                            ltem = sensor[0];
                        }
                        else if (ltem > sensor[0]) {
                            ltem = sensor[0];
                        }
                        if (hhum == null) {
                            hhum = sensor[1];
                        }
                        else if (hhum < sensor[1]) {
                            hhum = sensor[1]
                        }
                        if (lhum == null) {
                            lhum = sensor[1];
                        }
                        else if (lhum > sensor[1]) {
                            lhum = sensor[1]
                        }

                        strhtem = '<div class="col-md-4" style="margin-left:-3px">最高 ' + htem + '℃</div>';
                        strltem = '<div class="col-md-4" style="margin-left:-3px">最低 ' + ltem + '℃</div>';
                        strhhum = '<div class="col-md-4" style="margin-left:-3px">最高 ' + hhum + '％</div>';
                        strlhum = '<div class="col-md-4" style="margin-left:-3px">最低 ' + lhum + '％</div>';
                        temperature = sensor[0];                      //温度值
                        humidity = sensor[1];                         //湿度值
                        lon = sensor[2];
                        lat = sensor[3];
                    });
                    var point = new BMap.Point(lon, lat);
                    geoc.getLocation(point, function (rs) {
                        var addComp = rs.addressComponents;
                        $("#address" + ind).html(addComp.city + addComp.district + addComp.street + addComp.streetNumber);
                    });
                    points.push(point);                    
                     var str = ' <div class="w-locon-two "> <div class="w-locon-twott"><div class="twott-number"><p>车牌号</p><p>' + va.CarNumber + '</p></div><div class="twott-title"> <p>始发地：<b></b></p><p>目的地：<b></b></p></div></div><div class="w-locon-twoup"><ul><li><h3>温度</h3><p>最高' + htem + '℃</p><p>最低' + ltem + '℃</p></li><li><h3>湿度</h3><p>最高' + hhum + '％</p><p>最低' + lhum + '％</p></li><li><h3>速度</h3><p>最高km/h</p><p>最低km/h</p></li></ul></div><div class="sanjiao"></div>';

                    label = new BMap.Label(str, { offset: new BMap.Size(-140, -200) });
                    label.setStyle({                                   //给label设置样式，任意的CSS都是可以的
                        //fontSize:"14px",               //字号
                        border: "0",                    //边
                        //textAlign:"center",            //文字水平居中显示
                        //lineHeight:"120px",            //行高，文字垂直居中显示
                        //background: "transparent",
                        background: "transparent",
                        //background:"url(images/tip_bg.png) no-repeat",    //背景图片！
                        cursor: "pointer"
                    });
                    addMarker(point, label);
                    if (ind == 0) {
                        map.centerAndZoom(point, 15);
                    }
                }
                else {
                    //alert("未查询到实时数据！");
                }
            },
            complete: function (x) {
               
            }
        });
    });
}

function addMarker(point, label) {
    var myIcon = new BMap.Icon("img/bus.png", new BMap.Size(50, 37));
    var marker = new BMap.Marker(point, { icon: myIcon });
    //var marker = new BMap.Marker(point);
    map.addOverlay(marker);
    marker.setLabel(label);
}
function getpoint(addValue) {
    // 创建地址解析器实例
    var myGeo = new BMap.Geocoder();
    // 将地址解析
    myGeo.getPoint(addValue, function (point) {
        if (point) {
            return point;
        }
    });
}
function showcar(index) {
    map.centerAndZoom(points[index], 15);
}